<?php
namespace App\Http\Controllers\Admin;

use App\DBs\Admin\FrontDB;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class FrontController extends BaseController
{
    public function __construct()
    {
        $this->dbFront = new FrontDB();
    }

    //列表
    public function getIndex(Request $request)
    {
        $res = $this->dbFront->getIndex();
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //修改分类
    public function setUpdate(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id'])&&$arys['id'] ? $arys['id'] : 0;
        $cn = isset($arys['cn'])&&$arys['cn'] ? $arys['cn'] : '';
        $en = isset($arys['en'])&&$arys['en'] ? $arys['en'] : '';
        $link = isset($arys['link'])&&$arys['link'] ? $arys['link'] : '';
        if (!$id || !$cn || !$en) {
            return $this->toJson(-2,'参数错误');
        }
        $upData = ['cn'=>$cn,'en'=>$en,'link'=>$link];
        $res = $this->dbFront->setUpdate($id,$upData);
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0,'success');
    }

    //页面语言
    public function getLangIndex(Request $request)
    {
        $res = $this->dbFront->getLangIndex();
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //首页横图
    public function getAdIndex()
    {
        $res = $this->dbFront->getAdIndex();
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //首页横图新增
    public function setAdAdd(Request $request)
    {
        $arys = $request->all();
        $img = isset($arys['img'])&&$arys['img'] ? $arys['img'] : '';
        if (!$img) {
            return $this->toJson(-2,'没有图片上传');
        }
        $img = Upload::getPathByBase64($img);
        $res = $this->dbFront->setAdAdd($img);
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0);
    }

    //首页横图删除
    public function setAdDel(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id'])&&$arys['id'] ? $arys['id'] : 0;
        $res = $this->dbFront->setAdDel($id);
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0);
    }

    //赞助商
    public function getSponsorIndex()
    {
        $res = $this->dbFront->getSponsorIndex();
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //首页横图新增
    public function setSponsorAdd(Request $request)
    {
        $arys = $request->all();
        $img = isset($arys['img'])&&$arys['img'] ? $arys['img'] : '';
        if (!$img) {
            return $this->toJson(-2,'没有图片上传');
        }
        $img = Upload::getPathByBase64($img);
        $res = $this->dbFront->setSponsorAdd($img);
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0);
    }

    //首页横图删除
    public function setSponsorDel(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id'])&&$arys['id'] ? $arys['id'] : 0;
        $res = $this->dbFront->setSponsorDel($id);
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0);
    }
}